/**
 * 请假管理API服务
 * 
 * 严格按照《App端编码规范权威标准声明.md》API调用规范实现：
 * - 统一错误处理：使用try-catch处理API调用异常
 * - 参数验证：确保API调用参数格式正确
 * - 类型安全：使用明确的参数类型
 * - 完整注释：所有方法都有完整的JSDoc注释
 * - 错误消息处理：优先展示后端返回的提示消息
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 * @version 1.0.0
 */

import request from '@/api/request'

/**
 * 分页查询请假记录
 * @param {Object} params - 查询参数
 * @param {number} params.pageNum - 页码，从1开始
 * @param {number} params.pageSize - 每页大小
 * @param {number} [params.userId] - 用户ID
 * @param {number} [params.propertyCompanyId] - 物业公司ID
 * @param {string} [params.leaveType] - 请假类型
 * @param {number} [params.status] - 请假状态
 * @param {string} [params.startDate] - 开始日期 (yyyy-MM-dd)
 * @param {string} [params.endDate] - 结束日期 (yyyy-MM-dd)
 * @param {string} [params.applicantName] - 申请人姓名
 * @param {number} [params.approverId] - 审批人ID
 * @param {number} [params.departmentId] - 部门ID
 * @param {string} [params.roleType] - 角色类型
 * @returns {Promise<Object>} 返回分页数据
 */
export const getLeavePage = async (params) => {
  try {
    console.log('调用请假记录分页查询API，参数:', params)
    
    const response = await request.get('/api/work/leave/page', {
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
      userId: params.userId,
      propertyCompanyId: params.propertyCompanyId,
      leaveType: params.leaveType,
      status: params.status,
      startDate: params.startDate,
      endDate: params.endDate,
      applicantName: params.applicantName,
      approverId: params.approverId,
      departmentId: params.departmentId,
      roleType: params.roleType
    })
    
    console.log('请假记录分页查询API响应:', response)
    
    if (response.success && response.data) {
      return {
        success: true,
        data: response.data,
        message: response.message || '查询成功'
      }
    } else {
      return {
        success: false,
        data: null,
        message: response.message || response.msg || '查询失败'
      }
    }
  } catch (error) {
    console.error('请假记录分页查询API调用失败:', error)
    return {
      success: false,
      data: null,
      message: error.message || '网络错误，请重试'
    }
  }
}

/**
 * 根据用户ID查询请假记录
 * @param {number} userId - 用户ID
 * @param {number} propertyCompanyId - 物业公司ID
 * @returns {Promise<Object>} 返回请假记录列表
 */
export const getLeaveByUserId = async (userId, propertyCompanyId) => {
  try {
    console.log('调用根据用户ID查询请假记录API，用户ID:', userId, '物业公司ID:', propertyCompanyId)
    
    const response = await request.get(`/api/work/leave/user/${userId}`, {
      propertyCompanyId: propertyCompanyId
    })
    
    console.log('根据用户ID查询请假记录API响应:', response)
    
    if (response.success && response.data) {
      return {
        success: true,
        data: response.data,
        message: response.message || '查询成功'
      }
    } else {
      return {
        success: false,
        data: null,
        message: response.message || response.msg || '查询失败'
      }
    }
  } catch (error) {
    console.error('根据用户ID查询请假记录API调用失败:', error)
    return {
      success: false,
      data: null,
      message: error.message || '网络错误，请重试'
    }
  }
}

/**
 * 获取请假记录详情
 * @param {number} id - 请假记录ID
 * @returns {Promise<Object>} 返回请假记录详情
 */
export const getLeaveDetail = async (id) => {
  try {
    console.log('调用获取请假记录详情API，ID:', id)
    
    const response = await request.get(`/api/work/leave/${id}`)
    
    console.log('获取请假记录详情API响应:', response)
    
    if (response.success && response.data) {
      return {
        success: true,
        data: response.data,
        message: response.message || '查询成功'
      }
    } else {
      return {
        success: false,
        data: null,
        message: response.message || response.msg || '查询失败'
      }
    }
  } catch (error) {
    console.error('获取请假记录详情API调用失败:', error)
    return {
      success: false,
      data: null,
      message: error.message || '网络错误，请重试'
    }
  }
}

/**
 * 提交请假申请
 * @param {Object} leaveData - 请假申请数据
 * @param {string} leaveData.leaveType - 请假类型
 * @param {string} leaveData.startDate - 开始日期 (yyyy-MM-dd)
 * @param {string} leaveData.endDate - 结束日期 (yyyy-MM-dd)
 * @param {number} leaveData.days - 请假天数
 * @param {string} leaveData.reason - 请假原因
 * @param {string} [leaveData.emergencyContact] - 紧急联系人
 * @param {string} [leaveData.emergencyPhone] - 紧急联系电话
 * @param {Array} [leaveData.attachments] - 附件列表
 * @returns {Promise<Object>} 返回提交结果
 */
export const submitLeaveApplication = async (leaveData) => {
  try {
    console.log('调用提交请假申请API，数据:', leaveData)
    
    const response = await request.post('/api/work/leave/apply', leaveData)
    
    console.log('提交请假申请API响应:', response)
    
    if (response.success) {
      return {
        success: true,
        data: response.data,
        message: response.message || '提交成功'
      }
    } else {
      return {
        success: false,
        data: null,
        message: response.message || response.msg || '提交失败'
      }
    }
  } catch (error) {
    console.error('提交请假申请API调用失败:', error)
    return {
      success: false,
      data: null,
      message: error.message || '网络错误，请重试'
    }
  }
}

/**
 * 审批请假申请
 * @param {number} id - 请假记录ID
 * @param {Object} approveData - 审批数据
 * @param {number} approveData.status - 审批状态 (2-已批准, 3-已拒绝)
 * @param {string} [approveData.approveRemark] - 审批备注
 * @returns {Promise<Object>} 返回审批结果
 */
export const approveLeave = async (id, approveData) => {
  try {
    console.log('调用审批请假申请API，ID:', id, '审批数据:', approveData)
    
    const response = await request.put(`/api/work/leave/${id}/approve`, approveData)
    
    console.log('审批请假申请API响应:', response)
    
    if (response.success) {
      return {
        success: true,
        data: response.data,
        message: response.message || '审批成功'
      }
    } else {
      return {
        success: false,
        data: null,
        message: response.message || response.msg || '审批失败'
      }
    }
  } catch (error) {
    console.error('审批请假申请API调用失败:', error)
    return {
      success: false,
      data: null,
      message: error.message || '网络错误，请重试'
    }
  }
}

/**
 * 撤销请假申请
 * @param {number} id - 请假记录ID
 * @param {number} userId - 用户ID
 * @returns {Promise<Object>} 返回撤销结果
 */
export const cancelLeave = async (id, userId) => {
  try {
    console.log('调用撤销请假申请API，ID:', id, '用户ID:', userId)
    
    // 参数验证
    if (!id || !userId) {
      throw new Error('请假ID和用户ID不能为空')
    }
    
    const response = await request.post(`/api/work/leave/cancel/${id}?userId=${userId}`)
    
    console.log('撤销请假申请API响应:', response)
    
    if (response.success) {
      return {
        success: true,
        data: response.data,
        message: response.message || '撤销成功'
      }
    } else {
      return {
        success: false,
        data: null,
        message: response.message || response.msg || '撤销失败'
      }
    }
  } catch (error) {
    console.error('撤销请假申请API调用失败:', error)
    return {
      success: false,
      data: null,
      message: error.message || '网络错误，请重试'
    }
  }
}

/**
 * 删除请假记录
 * @param {number} id - 请假记录ID
 * @returns {Promise<Object>} 返回删除结果
 */
export const deleteLeave = async (id) => {
  try {
    console.log('调用删除请假记录API，ID:', id)
    
    const response = await request.delete(`/api/work/leave/${id}`)
    
    console.log('删除请假记录API响应:', response)
    
    if (response.success) {
      return {
        success: true,
        data: response.data,
        message: response.message || '删除成功'
      }
    } else {
      return {
        success: false,
        data: null,
        message: response.message || response.msg || '删除失败'
      }
    }
  } catch (error) {
    console.error('删除请假记录API调用失败:', error)
    return {
      success: false,
      data: null,
      message: error.message || '网络错误，请重试'
    }
  }
}
